package guis.classroom;

import exceptions.RepositoryException;
import guis.RegisterPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import classes.Classroom;
import classes.Iterator;
import classes.Member;

import communication.Facade;

public class FindClassroomWindow extends ClassroomWindow
{
	private static final long	serialVersionUID	= 1032307379765488755L;
	
	public FindClassroomWindow( JFrame source, RegisterPanel< Classroom > registerPanel, Facade facade )
	{
		super( "Buscar Classe", "buscar", source, registerPanel, facade );
		createActions();
	}
	
	private void createActions()
	{
		actionButton.addActionListener( new ActionListener()
		{

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				String name = nameTextField.getText();
				String magazine = magazineTextFild.getText();
				Member titular = ( Member ) titularComboBox.getSelectedItem();
				Member auxiliar = ( Member ) auxiliarComboBox.getSelectedItem();
				int titularId = 0;
				int auxiliarId = 0;
				if ( titular != null )
					titularId = titular.getId();
				if ( auxiliar != null )
					auxiliarId = auxiliar.getId();

				Classroom classroom = new Classroom( name, magazine, titularId, auxiliarId );

				try
				{
					Iterator< Classroom > iterator = facade.findClassroom( classroom );
					( ( ClassroomPanel ) registerPanel ).loadTable( iterator );
					setVisible( false );
				}
				catch ( RepositoryException e )
				{

				}
			}

		} );
		
		cancelButton.addActionListener( new ActionListener(){

			@Override
			public void actionPerformed( ActionEvent arg0 )
			{
				setVisible( false );
			}
			
		});
	}
}